package com.xjzd.spring.converter;

import com.xjzd.spring.validated.User;
import org.springframework.core.convert.converter.Converter;
import org.springframework.format.Formatter;

import java.util.Locale;
import java.util.Objects;

/**
 * 不推荐使用
 */
public class ConverterFormatter implements Formatter<User> {
    private final Converter<String, User> converter;

    public ConverterFormatter(Converter<String, User> converter) {
        this.converter = converter;
    }

    @Override
    public User parse(String text, Locale locale) {
        return Objects.requireNonNull(converter.convert(text));
    }

    @Override
    public String print(User object, Locale locale) {
        return object.toString(); // 根据需求实现反向转换
    }
}